package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.ToolCheckEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.FreeApi_PopUtils;
import com.glory.dgcpcpda.utils.FreeUI_GeneralPop;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EqpHoldActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    //设备号
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    //HOLD/暂停
    @BindView(R.id.rbHold)
    RadioButton rbHold;
    //REALEASE/放行
    @BindView(R.id.rbRealease)
    RadioButton rbRealease;
    //HOLD原因
    @BindView(R.id.etReason)
    FreeUI_ClearEditText etReason;
    //HOLD备注
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    //提交按钮
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.flEqpHoldList)
    FrameLayout flEqpHoldList;
    @BindView(R.id.ivEqpHoldArrow)
    ImageView ivEqpHoldArrow;
    @BindView(R.id.etEqpHold)
    TextView etEqpHold;
    @BindView(R.id.tvEqpHold)
    TextView tvEqpHold;

    private String mType = "";
    private List<String> mEqpHold = new ArrayList<>();
    private FreeUI_GeneralPop mEqpHoldPop;

    private FreeUI_GeneralFragmentDialog dialog;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_hold;
    }

    @Override
    protected void initView() {
        tvTitle.setText("设备HOLD");
        tvBaseSubmit.setText("提交");
        mType = "HOLD";
        setMainLayout(llMain);
        requestEqpHoldList();
    }

    private void requestEqpHoldList(){
        FreeUI_EntityCallBack<BaseResponseBean<EqpInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpInfoEntity>>
                (new TypeToken<BaseResponseBean<EqpInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    EqpInfoEntity body = response.body().getResponse().getBody();
                    mEqpHold = body.getHOLDCODELIST();
                    mEqpHoldPop = null;  // 重置菜单
                    etEqpHold.setText(mEqpHold.get(0));
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TRANSFERMES");

            }
        };
        JsonObject map = new JsonObject();
        if(mType.equals("HOLD")){
            map.addProperty("ACTIONTYPE", "EQPHOLDLIST");
        }else {
            map.addProperty("ACTIONTYPE", "EQPRELEASELIST");
        }
        NetUtils.requestPostNet("CPC.EQPQUERY","TRANSFERMES",this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.rbHold,R.id.rbRealease,R.id.flEqpHoldList})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.rbHold:
                mType = "HOLD";
                etReason.setText("");
                etComment.setText("");
                tvEqpHold.setText("HOLD码：");
                etEqpHold.setHint("请选择HOLD码");
                requestEqpHoldList();
                break;
            case R.id.rbRealease:
                mType = "REALEASE";
                etReason.setText("");
                etComment.setText("");
                tvEqpHold.setText("REALEASE码：");
                etEqpHold.setHint("请选择REALEASE码");
                requestEqpHoldList();
                break;
            case R.id.flEqpHoldList:
                if (mEqpHold.size() <= 0) {
                    showToast("未获取到设备"+mType+"列表", false);
                    return;
                }
                if (mEqpHoldPop != null) {
                    if (mEqpHoldPop.isShowing()) {
                        mEqpHoldPop.dismiss();
                    }
                } else {
                    mEqpHoldPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpHoldList.getWidth(), mEqpHold, ivEqpHoldArrow, (codeS, i) -> {
                        etEqpHold.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivEqpHoldArrow);
                mEqpHoldPop.showAsDropDown(flEqpHoldList, 0, 0);
                break;
            case  R.id.tvBaseSubmit:
                if(getText(etEqpID).equals(null)||getText(etEqpID).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }
//                if(getText(etReason).equals("")){
//                    showToast("HOLD原因不能为空",false);
//                    return;
//                }
//                if(getText(etComment).equals("")){
//                    showToast("HOLD备注不能为空",false);
//                    return;
//                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ToolCheckEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ToolCheckEntity>>
                                (new TypeToken<BaseResponseBean<ToolCheckEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ToolCheckEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showConfirmDialog("提交成功");
                                    etEqpID.setText("");
                                    etReason.setText("");
                                    etComment.setText("");
                                    etEqpID.requestFocus();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ToolCheckEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TRANSFERMES");

                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE",mType);
                        map.addProperty("ACTIONCODE",etEqpHold.getText().toString());
                        map.addProperty("EQUIPMENTID",getText(etEqpID));
                        map.addProperty("ACTIONREASON",getText(etReason));
                        map.addProperty("ACTIONCOMMENT",getText(etComment));
                        NetUtils.requestPostNet("CPC.CPCEQPHOLD","TRANSFERMES",this,map,callBack);
                    }
                });
        }
    }
}
